# coding:utf-8
"""
description:scene dashboard页面
"""
import os
import random

from common.Log import LOG
from pages.Public_elements import public_elements
from common.Gesture_Mainpulation import gesture_mainpulation
from appium.webdriver.common import mobileby

from common.Get_Yaml import get_yaml
from config.GlobalParameter import TEST_Element_YAML

dataElements = get_yaml(os.path.join(TEST_Element_YAML, "Dashboard.yaml"))


class dashboard_page(public_elements):
    by = mobileby.MobileBy()

    # 点击左上角三杆
    homeicon_loc = (by.ACCESSIBILITY_ID, dataElements.get_elementInfo(0))

    def click_homeicon(self):
        """ 点击左上角三杆"""
        self.find_element(*self.homeicon_loc).click()

    # 获取dashboard home name
    homename_loc = (by.ID, dataElements.get_elementInfo(1))

    def get_homename(self):
        """获取dashboard home name"""
        return self.find_element(*self.homename_loc).text

    # 点击Notifications 图标
    DashboardNotifs_loc = (by.ID, dataElements.get_elementInfo(2))

    def click_DashboardNotifs(self):
        """ 点击Notifications 图标"""
        self.find_element(*self.DashboardNotifs_loc).click()

    #  点击Add Device 图标，页面没有device时见Public.yaml（6）
    DashboardAdd_loc = (by.ID, dataElements.get_elementInfo(3))

    def click_DashboardAdd(self):
        """  点击Add Device 图标，页面没有device时则点击Public.yaml（6）"""
        self.find_element(*self.DashboardAdd_loc).click()

    # Dashboard上面Device ICon集合.无法断言icon和在离线状态
    DashboardDeviceIcon_loc = (by.ID, dataElements.get_elementInfo(4))

    def get_DashboardDeviceIcon(self):
        """Dashboard上面Device ICon集合.无法断言icon和在离线状态"""
        return self.find_elements(*self.DashboardDeviceIcon_loc)

    """Dashboard上面Device Name集合，点击跳转setting页面或获取device name
    可以拖动device 控件，默认最后一个是‘Add device’按钮"""
    DashboardDeviceName_loc = (by.ID, dataElements.get_elementInfo(5))

    def get_DashboardDeviceName(self):
        """Dashboard上面Device Name集合，点击跳转setting页面或获取device name
        可以拖动device 控件，默认最后一个是‘Add device’按钮"""
        return self.find_elements(*self.DashboardDeviceName_loc)

    def get_DashboardDevFN(self):
        """返回Dashboard上面Device Name集合，如果device则返回null(主要是为func_SelectDashboardDev()辅助)"""
        if super().assertVisible(*self.DashboardDeviceName_loc):
            LOG.info("返回Dashboard page 里面Device Name集合第一个。。。")
            return self.find_elements(*self.DashboardDeviceName_loc)[0].text
        else:
            LOG.info("Dashboard page 里没有Device Name，返回''。。。")
            return ""

    def func_clickAddDevice(self):
        """点击右上角icon，或没有room下有device的最后一个icon，或没device的Add Device按钮"""

        if random.randint(2, 2) == 1:
            LOG.info("点击右上角icon，跳转select room 弹框。。。")
            self.click_DashboardAdd()
            # 滑动Select Room框
        else:
            if self.assertVisible(*self.DashboardDeviceName_loc):
                try:
                    for index, row in enumerate(self.get_DashboardDeviceName()):
                        if row.text == "Add Device":
                            LOG.info("room tab下有device，点击最后一个‘Add Device’icon，直接跳转add device list page。。。")
                            self.get_DashboardDeviceName()[index].click()  # 点击后直接跳转add device list
                            return
                except:
                    LOG.error("在room tab page下没有找到‘Add Device’icon，无法测试，跳过该测试步骤。。。")
                    return
            else:
                LOG.info("room tab下没有device时底部的Add Device按钮，跳转select room 弹框。。。")
                super().click_button()  # dashboard页面没有device时的‘Add Device’按钮
        # 滑动Select Room框
        super().swipe_designSheet()
        # 随机选择room跳转add device页面
        random.choice(super().get_availableRoomName()).click()

    # 滑动dashboard上面room一行控件
    DashboardRooms_loc = (by.ID, dataElements.get_elementInfo(6))

    def click_DashboardRooms(self):
        """点击dashboard上面room一行控件的"""
        Rooms = self.find_elements(*self.DashboardRooms_loc)
        Rooms[1].click()

    # 左右滑动切换room page
    CutDashboardRooms_loc = (by.ID, dataElements.get_elementInfo(9))

    def get_CutDashboardRooms(self):
        """返回元素，看情况左右滑动"""
        return self.find_element(*self.CutDashboardRooms_loc)

    def func_SelectDashboardDev(self, DevName):
        """

        循环滑动dashboard页面，直到找到指定Device并点击后结束循环
        :param DevName: 想要点击的Device name
        :return:
        """
        Num = 1  # 左右滑动的变量
        for i in range(20):
            FirstDev = self.get_DashboardDevFN()  # 获取room tab下的第一个Dev name，没有则为null
            # text = 0  # 关闭大循  环的变量
            if super().assertVisible(*self.DashboardDeviceName_loc):  # 有设备页面，才进入此步骤
                for index, row in enumerate(self.get_DashboardDeviceName()):
                    print(DevName, row.text)
                    if DevName in row.text:
                        # text = 2  # 当找到指定Device后则赋值为1，才能跳出页面滑动循环
                        row.click()
                        LOG.info("已选择到指定DevName>%s并点击跳转页面,退出Device Name循环" % DevName)
                        return  # 直接结束两个循环跳出大方法
            #             break
            # if text == 2:
            #     LOG.info("已选择到指定DevName>%s并点击跳转页面,退出循环滑动Room Tab。进行其他测试》》》" % DevName)
            #     break
            if Num == 1:
                LOG.info("循环向右滑动Rooms tab，划到底是第一个room。")
                gesture_mainpulation().swipe_right(self.driver, self.get_CutDashboardRooms())
            elif Num == 2:
                LOG.info("循环向左滑动Rooms tab，划到底是最后一个room。")
                gesture_mainpulation().swipe_left(self.driver, self.get_CutDashboardRooms())
                if self.get_DashboardDevFN() == FirstDev:
                    LOG.error("循环滑动所有Room都没有找到指定DevName>%s，请检查DevName是否正确》》》" % DevName)
            if self.get_DashboardDevFN() == FirstDev:
                LOG.info("已滑到底，滑动前后DevName为{}={}，开始向左滑>>>".format(FirstDev, self.get_DashboardDevFN()))
                Num = 2  # 重新赋值，开始向左划

    # 获取room或者floor数量和文字，并点击
    FloorAndRoomName_loc = (by.ID, dataElements.get_elementInfo(7))

    def get_FloorAndRoomName(self):
        """获取room或者floor数量和文字，并点击"""
        return self.find_elements(*self.FloorAndRoomName_loc)

    # 滑动dashboard上面Floor一行控件
    DashboardFloors_loc = (by.ID, dataElements.get_elementInfo(8))

    def swipe_DashboardFloors(self):
        """滑动dashboard上面Floor一行控件"""
        gesture_mainpulation().swipe_left(self.driver, self.find_element(*self.DashboardFloors_loc))
